package com.bbz.core.concurrency.work8;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**1. 倒计时器CountDownLatch
 * 
 * CountDownLatch的方法不是很多，将它们一个个列举出来：
 * 
 * await() throws InterruptedException：调用该方法的线程等到构造方法传入的N减到0的时候，才能继续往下执行；
 * await(long timeout, TimeUnit unit)：与上面的await方法功能一致，只不过这里有了时间限制，调用该方法的线程等到指定的timeout时间后，
 * 不管N是否减至为0，都会继续往下执行； 
 * countDown()：使CountDownLatch初始值N减1； long
 * getCount()：获取当前CountDownLatch维护的值；
 * 
 * @author binbin.a.zhang
 *
 */
public class CountDownLatchDemo {

	private static CountDownLatch startSignal = new CountDownLatch(1);
	// 用来表示裁判员需要维护的是6个运动员
	private static CountDownLatch endSignal = new CountDownLatch(6);

	public static void main(String[] args) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(6);
		for (int i = 0; i < 6; i++) {
			executorService.execute(() -> {
				try {
					System.out.println(Thread.currentThread().getName() + " 运动员等待裁判员响哨！！！");
					startSignal.await();
					System.out.println(Thread.currentThread().getName() + "正在全力冲刺");
					endSignal.countDown();
					System.out.println(Thread.currentThread().getName() + "  到达终点");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		System.out.println("裁判员发号施令啦！！！");
		startSignal.countDown();
		endSignal.await();
		System.out.println("所有运动员到达终点，比赛结束！");
		executorService.shutdown();
	}
}
